// ActionScript file
package

{
	
import flash.display.Shape;
import flash.display.Sprite;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.utils.*;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.*;
import flash.geom.*;
import flash.filters.*;
import flash.errors.IOError;
import mx.controls.Alert;
import mx.controls.ComboBox;

	public class audioManager extends Sprite

	{

		private var _barArray: Array;
		private var _lineArray: Array;
		private var _sound: Sound;
		private var _songIndex: Number;
		private var _id3Index: Number;
		private var _sc: SoundChannel;
		private var _ba: ByteArray;
		private var _numBars: int;
		private var isplaying: int;
		private var _playtime: int;
		private var id3Array: Array;
		private var songArray: Array;
		
		public function audioManager()	
		{
	
		// the song array contains your mp3 files. Change them to reflect the mp3 you wish to play
		// to add more mp3 files add another array indice eg., songArray[3] = "somesong.mp3";
		songArray = new Array();
		songArray[0] = "assets/Darude - Ecstacy.mp3";
		songArray[1] = "assets/Darude - SandStorm.mp3";
		_songIndex = 0;
		_barArray = new Array();
		_lineArray = new Array();
		_sc = new SoundChannel();
		_sound = new Sound();
		_sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		_ba = new ByteArray();
		_playtime = 0;
		// change the number of bars
		_numBars = 60;
		createSpectrumBars();
		_sound.load(new URLRequest(songArray[_songIndex]));
		isplaying = 0;
		_id3Index = 0;
		_sc = _sound.play(_playtime, 25);
		var voltransform: SoundTransform = _sc.soundTransform;
		// set your volume for the mp3
		voltransform.volume = 0.6;
		_sc.soundTransform = voltransform;
		addEventListener(Event.ENTER_FRAME, animateBars);
		addEventListener(Event.ENTER_FRAME, getInfo);
		}
	
		private function createSpectrumBars(): void	
		{	
			for (var i: int = 0; i < _numBars; i++)
			{
				// this function allows you to change the the appearance of the bars
				var bar: Shape = new Shape();	
				var dropShadow: DropShadowFilter = new DropShadowFilter(6, 25, 0x00FF00, 1, 10, 10, 2, 1);	
				bar.graphics.beginFill(0xFF0099);	
				bar.graphics.drawRect(250, - 75, 1, 30);	
				bar.graphics.endFill();	
				bar.filters = [dropShadow];	
				bar.y = bar.height + 130;	
				bar.x = 1+i * 4;	
				addChild(bar);	
				_barArray.push(bar);	
				// this function allows you to change the the appearance of the blue dots	
				var line1: Shape = new Shape();	
				line1.graphics.beginFill(0x66FFFF);	
				line1.graphics.drawRect(250, - 90, 1, 2);	
				line1.graphics.endFill();	
				line1.y = bar.height + 95;	
				line1.x = 1+i * 4;
				addChild(line1);	
				_lineArray.push(line1);	
			}	
		}
			
		private function ioErrorHandler(event: IOErrorEvent): void{
	
		}
	
		public function onStop():void
		
		{
		
		_playtime = 0;
		
		_sc.stop();
		
		isplaying = 1;
		
		}
	
		public function onPlay():void
		
		{
		
			if (isplaying == 0)
		
			{
		
			// do nothing
		
			}
		
			else
		
			{
		
			_sc = _sound.play(_playtime, 25);
		
			isplaying = 0;
		
			}
		
		}
		
		public function onPause():void
		{	
			_playtime = _sc.position;
			_sc.stop();	
			isplaying = 1;
		}
		
		private var textField: TextField = new TextField();
		
		private function getInfo(event: Event): void
		
		{
		
		// stores the MP3 ID3 info into an array, you can add more id3 info by adding another id3Array
		
		id3Array = new Array();
		
		id3Array[0] = "Song: " + _sound.id3.songName + " ";
		
		id3Array[1] = "Artist: " + _sound.id3.artist + " ";
		
		id3Array[2] = "Album: " + _sound.id3.album + " ";
		
		if (_sound.id3.songName != null)
		
		{
		
		// formats the display and appearance of the id3 array
		
		
		
		textField.width = 190;
		
		textField.height = 20;
		
		textField.x = 50;
		
		textField.y = 100;
		
		addChild(textField);
		
		textField.text = id3Array[_id3Index];
		
		var format: TextFormat = new TextFormat();
		
		format.font = "Cambria";
		
		format.color = 0x0099CC;
		
		format.size = 13;
		
		textField.setTextFormat(format)
		
		}
		
		id3Timer.addEventListener("timer", cycleid3Array);
		
		id3Timer.start();
		
		}
		
		private var id3Timer: Timer = new Timer(8000, 10);
		
		private function cycleid3Array(event: TimerEvent): void
		
		{
		
		if (_id3Index != 2) // set to higher number if you use more id3 information in the arrays.
		
		{
		
		_id3Index++
		
		}
		
		else if (_id3Index != 0)
		
		{
		
		_id3Index = 0;
		
		}
		
		}
		
		
		
		public function cyclesongArray(dir:String,cBox:ComboBox):void
		
		{
		
			if (dir == 'forw' && _songIndex != 1) // set to higher number if you use more mp3 files in the songArray.	
			{
		
				_songIndex++;
				_sc.stop();	
				_sound = new Sound();	
				_sound.load(new URLRequest(songArray[_songIndex]));	
				_sc = _sound.play(_playtime, 25);	
				_sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);	
				var data:Number = _songIndex;	
				data++	
				cBox.selectedIndex = data	
			}else if (dir == 'rev' && _songIndex != 0)	
			{	
				_songIndex--;	
				_sc.stop();	
				_sound = new Sound();	
				_sound.load(new URLRequest(songArray[_songIndex]));	
				_sc = _sound.play(_playtime, 25);	
				_songIndex = 0;	
				var data1:Number = _songIndex;	
				data1++	
				cBox.selectedIndex = data1	
			}
		
		}
		
		public function setSong(data:Number):void
		{
			_songIndex = data;
		
			_sc.stop();
		
			_sound = new Sound();
			
			if(songArray[_songIndex] != "")
			{
		
			_sound.load(new URLRequest(songArray[_songIndex]));
		
			_sc = _sound.play(_playtime, 25);
		
			_sound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			//data++
			
			//Box.selectedIndex = data
			
			}else{
				mx.controls.Alert.show("Song doesn't exist!");
			}
		}
		
		private function animateBars(event: Event): void
		
		{
		
			SoundMixer.computeSpectrum(_ba, true, 0);
		
			for (var i: int = 0; i < _numBars; i++)	
			{
		
				_barArray[i].scaleY = _ba.readFloat();
		
				_lineArray[i].scaleY = _ba.readFloat();
		
			}
		
		}
	
	}

}